//
//  PicPickerCollection.swift
//  WBSwift
//
//  Created by maker on 2016/11/19.
//  Copyright © 2016年 maker. All rights reserved.
//

import UIKit
import SnapKit

class PicPickerCollection: UICollectionView {
     fileprivate let itemWidth = (screenWidth - 4*margin) / 3
     var images : [UIImage] = [UIImage](){
        didSet{
            if images.count > 9 {
                images = oldValue
            }
            reloadData()
        }
    }
    

    override func awakeFromNib() {
        super.awakeFromNib()
        
        dataSource = self
        register(UINib.init(nibName: "PicPickerCollectionCell", bundle: nil), forCellWithReuseIdentifier: "picPickerCell")
        
        //设置布局
        let layout = collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize.width = itemWidth
        layout.itemSize.height = itemWidth
        
        contentInset = UIEdgeInsetsMake(margin, margin, 0, margin)
        
        
    }

}

extension PicPickerCollection : UICollectionViewDataSource{

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if images.count == 9 {
            return 9
        }
        return images.count + 1
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{
        let cell  = collectionView.dequeueReusableCell(withReuseIdentifier: "picPickerCell", for: indexPath) as! PicPickerCollectionCell
        cell.image = indexPath.item < images.count ? images[indexPath.item] : nil
        return cell
    }

}
